<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>实例管理</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <script src="/qrcode/jquery-3.5.1.js"></script>
    <style>
        .layui-table-box button {
            font-size: 14px !important;
            height: 25px;
            line-height: 23px;
        }

        .pear-container {
            margin: 15px;
            width: calc(100vw - 30px);
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card searchCriteria">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">连接名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="请输入连接名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">主机</label>
                    <div class="layui-input-inline">
                        <input type="text" name="host" placeholder="请输入主机" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">端口</label>
                    <div class="layui-input-inline">
                        <input type="text" name="port" placeholder="请输入端口" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit
                            lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="mysqlTable" lay-filter="mysqlTable"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" style="background-color: #2DBBF0 !important;
                              border: 1px solid #2DBBF0 !important;" lay-event="update">
        编辑
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" style="background-color: #398FFF !important;
                              border: 1px solid #398FFF !important;" lay-event="backup">
        备份
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
        删除
    </button>
</script>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>

<script type="text/javascript" th:inline="javascript">
    layui.use(['jquery', 'form', 'table', 'layer'], function () {
        const form = layui.form,
            table = layui.table,
            layer = layui.layer;
        let cols = [
            [
                {field: 'name', title: '连接名称', align: 'center'},
                {field: 'class_name', title: '驱动', align: 'center'},
                {field: 'host', title: '主机', align: 'center'},
                {field: 'port', title: '端口', align: 'center'},
                {field: 'username', title: '用户名', align: 'center'},
                {field: 'password', title: '密码', align: 'center'},
                {field: 'remark', title: '备注', align: 'center'},
                {field: 'create_time', minWidth: 180, title: '创建时间', align: 'center'},
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 220}
            ]
        ]

        tableIns = table.render({
            elem: '#mysqlTable',
            url: '/backup/node/info/list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            id: 'mysqlTable',
            height: 'full-173',
            //解决页码的问题
            done: function (res) {
                // 获得当前页码
                var brforeCurr = tableIns.config.page.curr;
                // 获得当前页的记录数
                var dataLength = table.cache['mysqlTable'].length;
                var count = res.count; // 获得总记录数
                //如果当前页的记录数为0并且总记录数不为0
                if (dataLength == 0 && count != 0) {
                    // 刷新表格到上一页
                    table.reload("mysqlTable", {
                        page: {
                            curr: brforeCurr - 1
                        }
                    });
                }
            }
        });

        table.on('toolbar(mysqlTable)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        //新增mysql连接信息
        window.add = function () {
            parent.layer.open({
                title: ['新增', 'font-size: 18px;'],
                type: 2,
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                area: ['40%', '100%'],
                shade: 0.5,
                shadeClose: true,
                maxmin: true,
                id: 'addNewEquipment',
                content: '/backup/node/info/add',
                end: function () {
                    table.reload('mysqlTable');
                }
            });
        }

        table.on('tool(mysqlTable)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'update') {
                window.update(obj);
            } else if (obj.event === 'backup') {
                window.backup(obj);
            }
        });

        window.remove = function (obj) {
            layer.confirm('确定要删除该连接吗', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                $.ajax({
                    url: '/backup/node/info/delete',
                    type: 'post',
                    data: JSON.stringify({id: obj.data.id}),
                    dataType: 'json',
                    contentType: "application/json;charset=utf-8",
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg(res.message, {icon: 1});
                            table.reload('mysqlTable');
                        } else {
                            layer.msg(res.message, {icon: 2});
                        }
                    }
                });
            });
        }

        window.update = function (obj) {
            parent.layer.open({
                title: ['修改', 'font-size: 18px;'],
                type: 2,
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                area: ['40%', '100%'],
                shade: 0.5,
                shadeClose: true,
                maxmin: true,
                id: 'addNewEquipment',
                content: '/backup/node/info/update/' + obj.data.id,
                end: function () {
                    table.reload('mysqlTable');
                }
            });
        }

        window.backup = function (obj) {
            parent.layer.open({
                title: ['备份', 'font-size: 18px;'],
                type: 2,
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                area: ['40%', '100%'],
                shade: 0.5,
                shadeClose: true,
                maxmin: true,
                id: 'addNewEquipment',
                content: '/backup/node/info/node-info-backup/' + obj.data.id,
                end: function () {
                    table.reload('mysqlTable');
                }
            });
        }


        window.refresh = function (param) {
            table.reload('mysqlTable');
        }

        form.on('submit(user-query)', function (data) {
            table.reload('mysqlTable', {
                where: data.field,
                page: {
                    curr: 1 // 重新从第 1 页开始
                }
            });
            return false;
        });

    })
</script>
</body>
</html>